package com.service;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbSearcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class IpService {
    @Autowired
    private DbSearcher dbSearcher;

    /**
     * 根据ip获取ip是否在国内
     * @param ip
     * @return true:国内、false:国外
     * @throws IOException
     */
    public boolean getIpAddress (String ip) throws IOException {
        DataBlock dataBlock = dbSearcher.btreeSearch(ip) ;
        String[] arr = dataBlock.getRegion().split("\\|") ;
        if(arr[0].equals("中国") && !arr[2].equals("香港") && !arr[2].equals("澳门") && !arr[2].equals("台湾")) {
            // 国内处理
            return true;
        }else{
            // 国外处理
            return false;
        }
    }

}
